// pages/other//erweima/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    webviewUrl: '',
    plate: '',
    ad_id: '',
    haveHide: '',
    ra: '',
    ratio: '',
    step: '',
    challenge: '',
    ex_id: '',
    luckyid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    var openid = wx.getStorageSync('openid');
    if (options.plate == 1) {
      var webviewUrl = 'https://www.mnancheng.com/home/ads/ad?ad_id=' + options.ad_id + '&plate=' + options.plate + '&ratio=' + options.x_times + '&openid=' + openid
    } else {
      var webviewUrl = 'https://www.mnancheng.com/home/ads/ad?ad_id=' + options.ad_id + '&plate=' + options.plate + '&openid=' + openid
    }
    that.setData({
      webviewUrl: webviewUrl,
      plate: options.plate,
      ad_id: options.ad_id
    })
    if (options.plate == 1) {
      that.data.ra = options.ra
      that.data.ratio = options.x_times,
        that.data.step = options.step
    }
    if (options.rand_rlb) {
      that.data.ra = options.rand_rlb
    }
    if (options.plate == 8) {
      that.data.plate = options.plate
    }
    if (options.plate == 9||options.plate==10) {
      that.data.luckyid = options.luckyid;
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    const ad_id = that.data.ad_id;
    const plate = that.data.plate;
    var x_times = that.data.ratio;
    var ra = that.data.ra;
    var step = that.data.step;
    if (plate == 8 && that.data.haveHide == 5) {
      var ex_id = that.data.ex_id
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id,
        ex_id: ex_id,
        type: 2
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/Challenge/Challenge/adv_back',
        data: {
          unionid: wx.getStorageSync('openid'),
          ad_id: ad_id,
          ex_id: ex_id,
          type: 2,
          sign: sign
        },
        method: 'POST',
        success: function(res) {
          console.log(res.data.msg, res)
          let pages = getCurrentPages();
          if (pages.length > 1) {
            let prevPage = pages[pages.length - 2];
            if (res.data.code == 200) {
              prevPage.setData({
                erweima: 3,
              })
            } else {
              prevPage.setData({
                erweima: 4
              })
            }
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
      return
    } else if ((plate == 6 || plate == 7) && that.data.haveHide == 5) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        type: 2,
        ad_id: ad_id,
        plate: plate,
      }
      var signs = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/adauth/Adv/back_adv',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 2,
          ad_id: ad_id,
          plate: plate,
          sign: signs
        },
        method: "POST",
        header: {
          'Cache-Control': 'max-age=60,public', //60秒
        },
        success: function(res) {
          console.log(res.data.msg, res)
          let pages = getCurrentPages();
          if (pages.length > 1) {
            let prevPage = pages[pages.length - 2];
            if (res.data.code == 200) {
              prevPage.setData({
                erweima: 3,
                number: res.data.content,
              })
            } else {
              prevPage.setData({
                erweima: 4
              })
            }
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
      return
    } else if ((plate == 9 || plate == 10) && that.data.haveHide == 5) {
      var luckyid = that.data.luckyid;
      var objet = {
        unionid: wx.getStorageSync('openid'),
        id: ad_id,
        plate: plate,
        luckyid: luckyid,
        type: 2,
      }
      var sin = app.sort_ASCII(objet)
      wx.request({
        url: app.globalData.new_url + '/adauth/Advertising/oneself_ad',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 2,
          id: ad_id,
          plate: plate,
          luckyid: luckyid,
          sign: sin
        },
        method: "POST",
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log('二维码抽奖回调', res)
          let pages = getCurrentPages(); //获取页面栈
          if (pages.length > 1) {
            let prevPage = pages[pages.length - 2]; //上一页面（prevPage 就是获取的上一个页面的JS里面所有pages的信息
            if (res.data.code == 200) {
              prevPage.setData({
                erweima: 3,
              })
            } else {
              prevPage.setData({
                erweima: 4
              })
            }
            wx.navigateBack({
              delta: 1,
            })
          }
        },
      })
    } else if (that.data.haveHide == 5) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        type: 2,
        ad_id: ad_id,
        plate: plate,
        ra: ra,
        ratio: x_times,
        step: step
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/adauth/Adv/back_adv',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 2,
          ad_id: ad_id,
          plate: plate,
          ra: ra,
          ratio: x_times,
          step: step,
          sign:sign
        },
        method: 'POST',
        header: {
          'Cache-Control': 'max-age=60,public', //60秒
        },
        success: function(res) {
          console.log(res.data.msg, res)
          let pages = getCurrentPages(); //获取页面栈
          if (pages.length > 1) {
            let prevPage = pages[pages.length - 2]; //上一页面（prevPage 就是获取的上一个页面的JS里面所有pages的信息
            if (prevPage.route == "pages/main/turntable/index") {
              if (res.data.code === 200) {
                prevPage.setData({
                  erweima: 1,
                  number: res.data.content,
                })
              } else {
                prevPage.setData({
                  erweima: 2
                })
              }
              wx.navigateBack({
                delta: 1,
              })
            } else {
              if (res.data.code===200) {
                prevPage.setData({
                  erweima: 3,
                  number: res.data.content,
                })
              } else {
                prevPage.setData({
                  erweima: 4
                })
              }
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        }
      })
    }
  },

  onHide: function() {
    const that = this;
    that.data.haveHide = 5;
    const ad_id = that.data.ad_id;
    const plate = that.data.plate;
    if (plate == 8) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id,
        type: 1
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/Challenge/Challenge/adv_back',
        data: {
          unionid: wx.getStorageSync('openid'),
          ad_id: ad_id,
          type: 1,
          sign: sign
        },
        method: 'POST',
        success: function(res) {
          that.data.ex_id = res.data.ex_id
        }
      })
    }else if(plate==9 ||plate==10){
      var luckyid = that.data.luckyid;
      var objet = {
        unionid: wx.getStorageSync('openid'),
        id: ad_id,
        plate: plate,
        luckyid: luckyid,
        type: 1,
      }
      var sin = app.sort_ASCII(objet)
      wx.request({
        url: app.globalData.new_url + '/adauth/Advertising/oneself_ad',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 1,
          id: ad_id,
          plate: plate,
          luckyid: luckyid,
          sign: sin
        },
        method: "POST",
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data.msg, res)
        },
      })
    } else {
      var x_times = that.data.ratio;
      var ra = that.data.ra;
      var object = {
        unionid: wx.getStorageSync('openid'),
        type: 1,
        ad_id: ad_id,
        plate: plate,
        ra: ra,
        ratio: x_times
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/adauth/Adv/back_adv',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 1,
          ad_id: ad_id,
          plate: plate,
          ra: ra,
          ratio: x_times,
          sign:sign
        },
        method: 'POST',
        header: {
          'Cache-Control': 'max-age=60,public', //60秒
        },
        success: function(res) {
          console.log(res.data.msg, res)
        }
      })
    }
  },
})